﻿using UnityEngine;
using System.Collections;

public class ProjectileBehavior : MonoBehaviour {
	
	private Vector3 targetDest;
	//private Vector3 forceVector;
	private bool atDest;
	public GameObject Field;
	private const float variance = 5;
	
	// Use this for initialization
	void Start () {
		atDest = false;
		++(GameManager.numProjectiles);
        Debug.Log("Projectile Started");
	}
	
	// Update is called once per frame
	void Update () {
		Vector3 pos = this.transform.position;
		if(closeEnough(pos, targetDest)){
			kill();
		}
		if(pos.x < -65 || pos.x > 65 || pos.y < -31 || pos.y > 120){
			kill ();
		}
	}
	
	bool closeEnough(Vector3 vec1, Vector3 vec2){
		if(Mathf.Abs(vec1.x-vec2.x) < variance && Mathf.Abs(vec1.y-vec2.y) < variance)
			return true;
		return false;
	}
	
	public void setTarget(Vector3 targetPos){
		targetDest=targetPos;
	}
	
	public Vector3 getTarget(){
		return targetDest;
	}
	
	/*public void setForceVector(Vector3 fv){
		forceVector=fv;
	}*/
	
	void kill(){
		atDest = true;
		//--(GameManager.numProjectiles);

        targetDest.z = 0;
        Debug.Log(string.Format("({0},{1},{2}", targetDest.x, targetDest.y, targetDest.z));
        Instantiate(Resources.Load("Field"), targetDest, Quaternion.identity);
        Debug.Log("Kill called");
		Destroy (this.gameObject);
	}
	
	public bool isSploded(){
		return atDest;
	}
}
